package org.piax.dtngw;

import org.piax.dtngw.R;
import org.piax.dtngw.R.id;
import org.piax.ov.ovs.dtn.MessageDB;
import org.piax.ov.jmes.MessageData;
import org.piax.dtpubsub.DTPubSub;
//import org.piax.sample.*;

 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


//import org.piax.dtpubsub.DTPubSub;
//import org.piax.dtpubsub.SubscribeListener;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.util.Log;

public class USBSend2 extends Activity  {
     
    //�t�r�a����f�[�^��Activity�ɕ\�����邽�߂̃C���e���g
    public static final String RECV ="com.chocospj.USBSend2.Recv";
     
    IntentFilter intentFilter;
    RecvBroadcastReceiver receiver;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //�\�P�b�g�ʐM��M�̃T�[�r�X�N��
        startService(new Intent(this, USBSend2Service.class));
    }
     
    @Override
    protected void onResume() {
        super.onResume();
         
        //�u���[�h�L���X�g���V�[�o�̓o�^
        receiver = new RecvBroadcastReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(RECV);
        registerReceiver(receiver, intentFilter);  
    }
     
    @Override
    protected void onPause() {
        super.onPause();
        //�u���[�h�L���X�g���V�[�o�̉���
        unregisterReceiver(receiver);
    }
     
    public class RecvBroadcastReceiver extends BroadcastReceiver {

          @SuppressWarnings("null")
		@Override
          public void onReceive(Context context, Intent intent) {
             
            //�t�r�a����f�[�^��Activity�ɕ\������
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("message");

            TextView t = (TextView) findViewById(id.textView1);
            t.setText(message);
            waitTest(1000);

           //DTNに適当なメッセージをパブリッシュしてみる
            //long mid;
            Log.d("log", "first");
            //static DTPubSub test;
            DTPubSub dtp = null;
            String topic = "sample";
            String mes = "Message.";
            dtp.publish(topic, mes.getBytes());
            /*
            MessageDB test = null;
            MessageData txt = null;
            txt.KEY_TEXT = "Message.";
            //String topic = "sample";
            long mid = test.storeMessage(txt);
            */
            
            
            //PIAXDTNSample��activity��؂�ւ��Ă݂�
            Intent intent2 = new Intent();
            intent2.setClassName("org.piax.sample", "org.piax.sample.PIAXDTNSample");
            startActivity(intent2);
            
        }
    }
    
    //�E�F�C�g�p�N���X
    public synchronized void waitTest(long msec) {
    	try{
    		wait(msec);
    	}
    	catch(InterruptedException e) {
    		e.printStackTrace();
    	}
    }
}